package com.feihong.txgw.ui.mvp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.feihong.txgw.message.LoginMessage;
import com.feihong.txgw.ui.activity.base.BaseActivity;
import com.feihong.txgw.ui.activity.user.LoginActivity;
import com.feihong.txgw.ui.mvp.precenter.AbBasePrecenter;
import com.feihong.txgw.ui.mvp.view.IBaseActivity;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName BaseMVPCompateActivity
 * @package com.feihong.txgw.ui.mvp
 * @data 2018/5/3 16:51
 * @Describe
 * @Change
 */
public abstract class BaseMVPCompateActivity<P extends AbBasePrecenter> extends BaseActivity implements IBaseActivity {

    protected P mPresenter;

    @Override
    public void initView(Bundle savedInstanceState) {
        mPresenter = (P) initPresenter();
        if(mPresenter != null){
            mPresenter.attachMV(this);
        }
    }

    @Override
    public void bindData() {

    }

    @Override
    public void onUserLogin(LoginMessage loginMessage) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachMV();
        }
    }

    @Override
    public void showLoginDialog() {
        showConfirmDialog("未登录", "您还未登录，请先登录！", "登录", "取消",
                new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                        Intent loginIntent = new Intent();
                        loginIntent.setClass(getActivity(), LoginActivity.class);
                        startActivity(loginIntent);
                    }
                });
    }

    @Override
    public void checkLoginAndStartIntent(Intent intent) {
        if (isUserLogin()) {
            startActivity(intent);
        } else {
            showLoginDialog();
        }
    }

    @Override
    public void startNewActivity(@NonNull Class<?> clz) {
        startNewActivity(clz,null);
    }

    @Override
    public void startNewActivity(@NonNull Class<?> clz, Bundle bundle) {
        startNewActivityForResult(clz,bundle,-1);
    }

    @Override
    public void finishCurrentActivity() {
        finish();
    }

    @Override
    public Activity getCurrentActivity() {
        return getActivity();
    }
}
